#!/bin/bash
function dismenu() {
    while [ 1 ]
    do
        draw_border
        echo -e "\033[8;30H1) 练习打数字"
        echo -e "\033[9;30H2) 练习打字母"
        echo -e "\033[10;30H3) 练习字母数字混合"
        echo -e "\033[11;30H4) 练习打单词"
        echo -e "\033[12;30H5) 退出"
        echo -ne "\033[22;2HPlease input your choice: "
        read choice
        case $choice in
        "1")  draw_border
            main digit
            echo -e "\033[39;49m"
            ;;
        "2")  draw_border
            main char
            echo -e "\033[39;49m"
            ;;
        "3")  draw_border
            main mix
            echo -e "\033[39;49m"
            ;;
        "4") draw_border

            echo -ne "\033[22;2H"
            read -p "哪个文件你想用于打字游戏练习：" file
            if [ ! -f "$file" ];then
                dismenu
            else
                exec 4<$file #创建一个文件管道
                main word
                echo -e "\033[39;49m"
            fi
            ;;
        "5"|"q"|"Q")
            draw_border
            echo -e "\033[10;25Hyou will exit this game,now"
            echo -e "\033[39;49m"
            sleep 1
            clear
            exit 1
            ;;
        * )
            draw_border
            echo -e "\033[22;2Hyour choice is wrong,please try again"
            sleep 1
            ;;
        esac
    done


}

function draw_border() {
    declare -i width
    declare -j high
    width=79   #终端默认宽度为-1
    high=23    #终端默认高度-1
    clear
#设置显示颜色，黑底白字
    echo -e "\033[37;40m"
#设置背景颜色
    for (( i=1; i<=$width; i=i+1 ))
    do
        for (( j=1; j<=$high; j=j+1 ))
        do
        #设置显示位置
           echo -e "\033["$j";"$i"H "
        done

    done


    #画背景框
    echo -e "\033[1;1H+\033["$high";1H+\033[1;"$width"H+\033["$high";"$width"H+"
    for (( i=2; i<=$width-1; i=i+1 ))
    do
        echo -e "\033[1;"$i"H-"
        echo -e "\033["$high";"$i"H-"
    done
    for (( i=2; i<=$high-1; i=i+1 ))
    do
        echo -e "\033["$i";1H|"
        echo -e "\033["$i";"$width"H|\n"

    done


}
dismenu
